package com.lagou.filter;

import io.netty.handler.codec.http.cookie.Cookie;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpCookie;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Set;

public class TokenFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        MultiValueMap<String, HttpCookie> cookies = request.getCookies();
        if(cookies != null){
            Set<String> keySet = cookies.keySet();
            for(String cookie : keySet){
                List<HttpCookie> httpCookies = cookies.get(keySet);
                for(HttpCookie httpCookie:httpCookies){
                    if(httpCookie.getName().equals("token")){
                        String token = httpCookie.getValue();
                    }
                }
            }
        }
        return null;
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
